//
//  CollectionViewCellViewController.m
//  LNA
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "CollectionViewCellViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "CollectionModel.h"
#import "MyLoopsView.h"
#import "FirstViewController.h"
#import "WebViewController.h"
#import "HotCollectionViewCell.h"
#import "MoneyCell.h"
#import "MoneyModel.h"
#import "ButtonCollectionController.h"
#import "DestinationModel.h"
#import "buttonModel.h"
#import "CityViewController.h"


@interface CollectionViewCellViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIView *headerView; // tableView头高
@property (nonatomic, retain) NSMutableArray *scrollArr;// 轮播图图片数组
@property (nonatomic, retain) NSMutableArray *dataArr;//  轮播图图片的数据
@property (nonatomic, retain) NSString *overview_urlStr;// 属性传值webView
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *moneyArr;// tableView的数据

@property (nonatomic, retain) NSIndexPath *index;
@end

@implementation CollectionViewCellViewController

#pragma mark - 目的地热门城市点击跳转VC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
#pragma mark - tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    // cell 重用池
    [_tableView registerClass:[MoneyCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];

#pragma mark - tableView上的View
    UIView *ziyouxingView = [[UIView alloc] initWithFrame:CGRectMake(0, 460, self.view.frame.size.width, 30)];
    ziyouxingView.backgroundColor = [UIColor whiteColor];
    [_headerView addSubview:ziyouxingView];
    
#pragma mark - 超值自由行的label
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 460, 150, 30)];
    title.text = @"超值自由行";
    title.textColor = [UIColor colorWithRed:137 /255.0 green:137/255.0 blue:137/255.0 alpha:1];
    [_headerView addSubview:title];
    [ziyouxingView release];
    [title release];
    
    
#pragma mark - 轮播图调用方法
    [self pic];
}
#pragma mark - 轮播图
- (void)pic
{
#pragma mark - 判断 如果是城市 执行带collectionview
    if (!([self.cid  isEqualToString:@"62"] | [self.cid isEqualToString:@"10723"] | [self.cid isEqualToString:@"10239"] |[self.cid isEqualToString:@"7789"])) {
       
    // 获取轮播图数据
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@",self.cid];
        NSLog(@"~~%@",self.cid);
    NSString *url = @"&lat=38.88241944084977&lon=121.5398896720327&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22&track_os=ios%25209.2&v=1";
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:[NSString stringWithFormat:@"%@%@",str,url]parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
//        NSLog(@"%lld",downloadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
#pragma mark - tableView的header
        self.headerView = [[UIView alloc] initWithFrame:CGRectMake(10, 250, self.view.frame.size.width, 490)];
        _headerView.backgroundColor = [UIColor colorWithRed: 215 / 255.0 green:215 / 255.0 blue: 215 / 255.0 alpha:1];
        self.tableView.tableHeaderView = _headerView;
        [_headerView release];
        NSArray *arr = responseObject[@"data"][@"photos"];
        if (arr.count < 3) {
            UIImageView *backimg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
            backimg.image = [UIImage imageNamed:@"boom.jpg"];
            [_headerView addSubview:backimg];
            [backimg release];
//            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//            // 滚动方向
//            layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//            // cell大小
//            layout.itemSize = CGSizeMake(345 / 2, 170 / 2);
//            // 行列间距
//            layout.minimumInteritemSpacing = 10;
//            layout.minimumLineSpacing = 10;
//            // 边界
//            layout.sectionInset = UIEdgeInsetsMake(5, 10, 10, 10);
//            self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 250, self.view.frame.size.width, 200) collectionViewLayout:layout];
//            _collectionView.delegate = self;
//            _collectionView.dataSource = self;
//            _collectionView.backgroundColor = [UIColor whiteColor];
//            [self.headerView addSubview:_collectionView];
//            // 重用池
//            [_collectionView registerClass:[HotCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
//            [_collectionView release];
//            [layout release];
            
            
        }else{
        
#pragma mark - 轮播图数据处理
        self.scrollArr = responseObject[@"data"][@"photos"];
       // 轮播图
        MyLoopsView *scroll = [MyLoopsView myLoopsViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) imageArr:_scrollArr];
        // 添加在header上
        [_headerView addSubview:scroll];
            
#pragma mark - button 点击跳转国家实用信息的webView数据
        self.overview_urlStr = responseObject[@"data"][@"overview_url"];
#pragma mark - 热门城市的数据处理
        NSArray *hotArr = responseObject[@"data"][@"hot_city"];
        self.dataArr = [CollectionModel modelHanderWithArrar:hotArr];
#pragma mark - 获取超值自由行
        NSArray *mArr = responseObject[@"data"][@"new_discount"];
        self.moneyArr = [MoneyModel modelHanderWithArrar:mArr];
        [_tableView reloadData];
        [_collectionView reloadData];
#pragma mark - 轮播图上的label 中文 国家名
        UILabel *labelCnname = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, 120, 50)];
        [scroll addSubview:labelCnname];
        labelCnname.text = responseObject[@"data"][@"cnname"];
        labelCnname.font = [UIFont systemFontOfSize:18];
        labelCnname.textColor = [UIColor whiteColor];
        [labelCnname release];
#pragma mark -  轮播图上的label 英文 国家名
        UILabel *labelEnname = [[UILabel alloc] initWithFrame:CGRectMake(30, 110, 120, 30)];
        [scroll addSubview:labelEnname];
        labelEnname.text = responseObject[@"data"][@"enname"];
        labelEnname.textColor = [UIColor whiteColor];
        labelEnname.font = [UIFont systemFontOfSize:15];
        [labelEnname release];
                                
#pragma mark -  轮播图上的label 国家简介
        UILabel *labelEntryCont = [[UILabel alloc] initWithFrame:CGRectMake(30, 130, self.view.frame.size.width - 60, 60)];
        labelEntryCont.text = responseObject[@"data"][@"entryCont"];
        [scroll addSubview:labelEntryCont];
        labelEntryCont.font = [UIFont systemFontOfSize:14];
        labelEntryCont.numberOfLines = 2;
        labelEntryCont.textColor = [UIColor whiteColor];
        [labelEntryCont release];
        
#pragma mark -  国家使用信息按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(260, 170, 100, 30);
        [btn setTitle:@"国家实用信息 ☞" forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setTitleColor:[UIColor colorWithRed:0.4 green:0.7 blue:0.4 alpha:1] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(button) forControlEvents:UIControlEventTouchUpInside];
        [scroll addSubview:btn];
        
#pragma mark - collectionview创建
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 滚动方向
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        // cell大小
        layout.itemSize = CGSizeMake(345 / 2, 170 / 2);
        // 行列间距
        layout.minimumInteritemSpacing = 10;
        layout.minimumLineSpacing = 10;
        // 边界
        layout.sectionInset = UIEdgeInsetsMake(5, 10, 10, 10);
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 250, self.view.frame.size.width, 200) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [self.headerView addSubview:_collectionView];
        // 重用池
        [_collectionView registerClass:[HotCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
        [_collectionView release];
        [layout release];
       
;
#pragma mark - collectionView 上的view
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 210, self.view.frame.size.width, 40)];
        view.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:view];
        
        UIView *hengxianView = [[UIView alloc] initWithFrame:CGRectMake(10, 245, self.view.frame.size.width - 20, 1)];
        hengxianView.backgroundColor = [UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1];
        [_headerView addSubview:hengxianView];
        
        UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 215, 100, 30)];
        [_headerView addSubview:cityLabel];
        cityLabel.text = @"热门城市";
        cityLabel.textColor = [UIColor colorWithRed:137 /255.0 green:137/255.0 blue:137/255.0 alpha:1];
        [cityLabel release];
        [view release];
        [hengxianView release];
#pragma mark - collectioncView上的button
        UIButton *btnCollection = [UIButton buttonWithType:UIButtonTypeCustom];
        btnCollection.frame = CGRectMake(280, 215, 80, 30);
        
        [_headerView addSubview:btnCollection];
        [btnCollection setTitle:@"查看全部 ☞" forState:UIControlStateNormal];
        btnCollection.titleLabel.font = [UIFont systemFontOfSize:15];
        [btnCollection setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btnCollection addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
#pragma mark - tableView上的View
        UIView *ziyouxingView = [[UIView alloc] initWithFrame:CGRectMake(0, 460, self.view.frame.size.width, 30)];
        ziyouxingView.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:ziyouxingView];
        
        
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 460, 150, 30)];
        title.text = @"超值自由行";
        title.textColor = [UIColor colorWithRed:137 /255.0 green:137/255.0 blue:137/255.0 alpha:1];
        [_headerView addSubview:title];
        [ziyouxingView release];
        [title release];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
//        NSLog(@"%@",error);
    }];
    }else{
#pragma mark - 获取轮播图数据
        NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/city_detail?city_id=%@",self.cid];
        NSString *url = @"&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1";
        AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
        [sessionManager GET:[NSString stringWithFormat:@"%@%@",str,url]parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
//            NSLog(@"%lld",downloadProgress.totalUnitCount);
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
#pragma mark - tableViewheader
            self.headerView = [[UIView alloc] initWithFrame:CGRectMake(10, 250, self.view.frame.size.width, 240)];
            _headerView.backgroundColor = [UIColor colorWithRed: 215 / 255.0 green:215 / 255.0 blue: 215 / 255.0 alpha:1];
            self.tableView.tableHeaderView = _headerView;
            [_headerView release];
#pragma mark - 轮播图数据处理
           
            self.scrollArr = responseObject[@"data"][@"photos"];
           
            // 轮播图
            MyLoopsView *scroll = [MyLoopsView myLoopsViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) imageArr:_scrollArr];
            // 添加在header上
            [_headerView addSubview:scroll];
#pragma mark -  button 点击国家实用信息跳转的webView数据
           self.overview_urlStr = responseObject[@"data"][@"overview_url"];
            
#pragma mark -  获取超值自由行
            NSArray *mArr = responseObject[@"data"][@"new_discount"];
            self.moneyArr = [MoneyModel modelHanderWithArrar:mArr];
            [_tableView reloadData];
            [_collectionView reloadData];
            
#pragma mark -  轮播图上的label 中文 国家名
            UILabel *labelCnname = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, 120, 50)];
            [scroll addSubview:labelCnname];
            
            labelCnname.text = responseObject[@"data"][@"cnname"];
            labelCnname.font = [UIFont systemFontOfSize:18];
            labelCnname.textColor = [UIColor whiteColor];
            [labelCnname release];
            
#pragma mark -  轮播图上的label 英文 国家名
            UILabel *labelEnname = [[UILabel alloc] initWithFrame:CGRectMake(30, 110, 120, 30)];
            [scroll addSubview:labelEnname];
            labelEnname.text = responseObject[@"data"][@"enname"];
            labelEnname.textColor = [UIColor whiteColor];
            labelEnname.font = [UIFont systemFontOfSize:15];
            [labelEnname release];
            
#pragma mark -  轮播图上的label 国家简介
            UILabel *labelEntryCont = [[UILabel alloc] initWithFrame:CGRectMake(30, 130, self.view.frame.size.width - 60, 60)];
            labelEntryCont.text = responseObject[@"data"][@"entryCont"];
            [scroll addSubview:labelEntryCont];
            labelEntryCont.font = [UIFont systemFontOfSize:14];
            labelEntryCont.numberOfLines = 2;
            labelEntryCont.textColor = [UIColor whiteColor];
            [labelEntryCont release];
            
#pragma mark -  国家使用信息按钮
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(260, 170, 100, 30);
            [btn setTitle:@"国家实用信息 ☞" forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [btn setTitleColor:[UIColor colorWithRed:0.4 green:0.7 blue:0.4 alpha:1] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(button) forControlEvents:UIControlEventTouchUpInside];
            [scroll addSubview:btn];
            
#pragma mark - tableView上的View
            UIView *ziyouxingView = [[UIView alloc] initWithFrame:CGRectMake(0, 210, self.view.frame.size.width, 30)];
            ziyouxingView.backgroundColor = [UIColor whiteColor];
            [_headerView addSubview:ziyouxingView];
            
#pragma mark -  超值自由行
            UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 210, 150, 30)];
            title.text = @"超值自由行";
            title.textColor = [UIColor colorWithRed:137 /255.0 green:137/255.0 blue:137/255.0 alpha:1];
            [_headerView addSubview:title];
            [ziyouxingView release];
            [title release];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
//            NSLog(@"%@",error);
        }];
    }
   
}
#pragma mark - 点击查看全部 button点击方法
- (void)jump
{
    

    ButtonCollectionController *btnVC = [[ButtonCollectionController alloc] init];
    NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/place/city/get_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&countryid=%@",self.cid];
    NSString *str = @"&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1";
    NSString *xrsb = [NSString stringWithFormat:@"%@%@",url,str];
   btnVC.str = xrsb;
    [self.navigationController pushViewController:btnVC animated:YES];
    
    
    [btnVC release];
    
}
#pragma mark - 国家实用信息button点击方法
- (void)button
{
    
    WebViewController *webVC = [[WebViewController alloc] init];
    webVC.str = _overview_urlStr;
    [self.navigationController pushViewController:webVC animated:YES];
    [webVC release];
    
    
}
#pragma mark - tableView 方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _moneyArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoneyModel *m = _moneyArr[indexPath.row];
    MoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.photo]];
    cell.priceLabel.text = [[m.price stringByReplacingOccurrencesOfString:@"<em>" withString:@""] stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
    cell.titleLabel.text = m.title;
    cell.titleLabel.font = [UIFont systemFontOfSize:15];
    cell.titleLabel.numberOfLines = 0;
    cell.priceoffLabel.text = m.priceoff;
    cell.priceoffLabel.font = [UIFont systemFontOfSize:15];
    cell.priceoffLabel.textColor = [UIColor colorWithRed:137 /255.0 green:137/255.0 blue:137/255.0 alpha:1];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    WebViewController *firstVC = [[WebViewController alloc] init];
    MoneyModel *m = _moneyArr[indexPath.row];
    firstVC.str = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.8.3&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_user_id=7328550", m.cid];
    [self.navigationController pushViewController:firstVC animated:YES];
    
    
}
#pragma mark - collectionView 方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionModel *m = _dataArr[indexPath.row];
    HotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    cell.cnnameLabel.text = m.cnname;
    cell.ennameLabel.text = m.enname;
   [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.photo]];

 
    
    
    
    return cell;

}
#pragma mark - collectionCell跳转
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CityViewController *cityVC = [[CityViewController alloc] init];
    CollectionModel *m = _dataArr[indexPath.row];
    cityVC.str = m.strid;
    [self.navigationController pushViewController:cityVC animated:YES];
    
    [cityVC release];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
